#pragma once
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/types.h>

using namespace std;

typedef void(*work_t)();//函数指针类型
typedef void(*task_t)();

void task1()
{
    cout << "任务1" << endl;
}
void task2()
{
    cout << "任务2" << endl;
}
void task3()
{
    cout << "任务3" << endl;
}
task_t tasks[3] = {task1, task2, task3};

uint32_t NextTask()
{
    return rand() % 3;
}

//有了重定向，只需到标准输入即可
void worker()
{
    while(true)
    {   
        uint32_t command_code = 0;//随机任务下标
        ssize_t n = read(0, &command_code, sizeof(command_code));
        if(n == sizeof(command_code))
        {
            if(command_code >= 3) continue;
            tasks[command_code]();
        }
        cout<< "I am a worker, pid: " << getpid() << endl;
        sleep(1); 
    }
}